/**
 * Advanced Software Engineering - assignment part 1 (monica Farrow)
 * JUnit ConstructorTestParcel class
 * 
 * Some of the methods in this class have been based on lecture notes provided by
 * Monica Farrow as a part of Software Engineering courses at Heriot-Watt University.
 */

package depotTests;

import static org.junit.Assert.*;
import org.junit.Test;

import depot.*;

public class ProcessInputLineTestParcel {

	private ParcelUncollected pu;
	private String inputLine;

	/**
	 * Test element missing from input line
	 * Test only 5 elements
	 * Test for ArrayIndexOutOfBoundsException
	 * @throws DuplicateIDException 
	 * @throws IllegalArgumentException 
	 * @throws NumberFormatException 
	 */
	@Test(expected = ArrayIndexOutOfBoundsException.class)
	public final void testProcInLineElements1_1() throws NumberFormatException, IllegalArgumentException, DuplicateIDException {
		// create ParcelUncollected object
		pu = new ParcelUncollected();
		// create input line
		inputLine = "E000112,20,30,40,2.6";
		pu.processLine(inputLine, 1);
	}


	/**
	 * Test element missing from input line
	 * Test only 5 elements
	 * Test for exception message
	 * @throws DuplicateIDException 
	 * @throws IllegalArgumentException 
	 * @throws NumberFormatException 
	 */
	@Test
	public final void testProcInLineElements1_2() throws NumberFormatException, IllegalArgumentException, DuplicateIDException {
		try {
			// create ParcelUncollected object
			pu = new ParcelUncollected();
			// create input line
			inputLine = "E000112,20,30,40,2.6";
			pu.processLine(inputLine, 1);
			fail("Too few elements - should throw exception");
		} catch (ArrayIndexOutOfBoundsException aio) {
			assertTrue(aio.getMessage().contains("5"));
		}
	}


	/**
	 * Test incorrect conversion of String to Integer
	 * Test 2nd element contains 'o'
	 * Test for NumberFormatException
	 * @throws DuplicateIDException 
	 * @throws IllegalArgumentException 
	 * @throws ArrayIndexOutOfBoundsException 
	 */
	@Test(expected = NumberFormatException.class)
	public final void testProcInLineNonInt1_1() throws ArrayIndexOutOfBoundsException, IllegalArgumentException, DuplicateIDException {
		// create ParcelUncollected object
		pu = new ParcelUncollected();
		// create input line
		inputLine = "E000112,2o,30,40,2.6,6";
		pu.processLine(inputLine, 1);
	}


	/**
	 * Test incorrect conversion of String to Integer
	 * Test 2nd element contains 'o'
	 * Test for exception message
	 * @throws DuplicateIDException 
	 * @throws IllegalArgumentException 
	 * @throws ArrayIndexOutOfBoundsException 
	 */
	@Test
	public final void testProcInLineNonInt1_2() throws ArrayIndexOutOfBoundsException, IllegalArgumentException, DuplicateIDException {
		try {
			// create ParcelUncollected object
			pu = new ParcelUncollected();
			// create input line
			inputLine = "E000112,2o,30,40,2.6,6";
			pu.processLine(inputLine, 1);
			fail("Non-integer in 2nd element - should throw exception");
		} catch (NumberFormatException nfe) {
			//			System.out.println(nfe.getMessage());
			assertTrue(nfe.getMessage().contains("2o"));
		}
	}


	/**
	 * Test incorrect conversion of String to Integer
	 * Test 6th element contains 'r'
	 * Test for NumberFormatException
	 * @throws DuplicateIDException 
	 * @throws IllegalArgumentException 
	 * @throws ArrayIndexOutOfBoundsException 
	 */
	@Test(expected = NumberFormatException.class)
	public final void testProcInLineNonInt2() throws ArrayIndexOutOfBoundsException, IllegalArgumentException, DuplicateIDException {
		// create ParcelUncollected object
		pu = new ParcelUncollected();
		// create input line
		inputLine = "E000112,20,30,40,2.6,r";
		pu.processLine(inputLine, 1);
	}


	/**
	 * Test incorrect conversion of String to Double
	 * Test 5th element contains 'e'
	 * Test for NumberFormatException
	 * @throws DuplicateIDException 
	 * @throws IllegalArgumentException 
	 * @throws ArrayIndexOutOfBoundsException 
	 */
	@Test(expected = NumberFormatException.class)
	public final void testProcInLineNonDbl() throws ArrayIndexOutOfBoundsException, IllegalArgumentException, DuplicateIDException {
		// create ParcelUncollected object
		pu = new ParcelUncollected();
		// create input line
		inputLine = "E000112,20,30,40,e.6,6";
		pu.processLine(inputLine, 1);
	}


	/**
	 * Test existence of Parcel with duplicate ID in parcelList
	 * Test 5th line contains duplicate ID
	 * Test for DuplicateIDException
	 * @throws NumberFormatException 
	 * @throws IllegalArgumentException 
	 * @throws ArrayIndexOutOfBoundsException 
	 * @throws DuplicateIDException 
	 */
	@Test(expected = DuplicateIDException.class)
	public final void testProcInLineDupID1_1() throws ArrayIndexOutOfBoundsException, IllegalArgumentException, NumberFormatException, DuplicateIDException {
		// create ParcelUncollected object
		pu = new ParcelUncollected();
		// create input line
		inputLine = "E000112,20,30,40,3.6,6";
		String inputLine2 = "E000113,20,30,40,3.6,6";
		String inputLine3 = "E000114,20,30,40,3.6,6";
		String inputLine4 = "E000115,20,30,40,3.6,6";
		String inputLine5 = "E000112,20,30,40,3.6,6";
		//process input lines
		pu.processLine(inputLine, 1);
		pu.processLine(inputLine2, 1);
		pu.processLine(inputLine3, 1);
		pu.processLine(inputLine4, 1);
		pu.processLine(inputLine5, 1);
	}


	/**
	 * Test existence of Parcel with duplicate ID in parcelList
	 * Test 5th line contains duplicate ID
	 * Test for exception message
	 * @throws NumberFormatException 
	 * @throws IllegalArgumentException 
	 * @throws ArrayIndexOutOfBoundsException 
	 */
	@Test
	public final void testProcInLineDupID1_2() throws ArrayIndexOutOfBoundsException, IllegalArgumentException, NumberFormatException {
		try {
			// create ParcelUncollected object
			pu = new ParcelUncollected();
			// create input line
			inputLine = "E000112,20,30,40,3.6,6";
			String inputLine2 = "E000113,20,30,40,3.6,6";
			String inputLine3 = "E000114,20,30,40,3.6,6";
			String inputLine4 = "E000115,20,30,40,3.6,6";
			String inputLine5 = "E000112,20,30,40,3.6,6";
			pu.processLine(inputLine, 1);
			pu.processLine(inputLine2, 1);
			pu.processLine(inputLine3, 1);
			pu.processLine(inputLine4, 1);
			pu.processLine(inputLine5, 1);
			fail("Duplicate ID in 5th line - should throw exception");
		} catch (DuplicateIDException die) {
			assertTrue(die.getMessage().contains("already exists"));
		}
	}


	/**
	 * Test all exceptions above
	 * Test - all elements correct, no dup IDs
	 */
	@Test
	public final void testProcInLineCorrect() {
		// create ParcelUncollected object
		pu = new ParcelUncollected();
		// create input line
		inputLine = "E000112,20,30,40,2.6,6";
		String inputLine2 = "E000113,20,30,40,3.6,6";
		try {
			pu.processLine(inputLine, 1);
			pu.processLine(inputLine2, 1);
		} catch (ArrayIndexOutOfBoundsException | IllegalArgumentException | DuplicateIDException e) {
			// do nothing!
			fail("No errors in inputLine - should NOT throw exception");
		}
	}


}
